from threading import Thread
from multiprocessing import Process, Pool
import requests, time

#建立请求函数
def requests_360(index):
    url = 'https://hao.360.com/?src=bm'
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 QIHU 360SE'}
    net = requests.get(url, headers=headers)
    print(f'第{index}次，requests 结果：{net.status_code}')
    time.sleep(0.5)


#建立多线程
t_arry=[]
if __name__ == '__main__':
    for index in range(10):
        t = Thread(target=requests_360, args=(index,))
        t.start()
        t_arry.append(t)
    n=time.time()

    for t in t_arry:
        t.join()
        y=time.time()
        print(float('%.2f' % (y-n)))

#建立多进程
if __name__ == '__main__':
    p_arry=[]
    for index in range(10):
        p = Process(target=requests_360, args=(index,))
        p.start()
        p_arry.append(p)
    cc = time.time()
    for i in p_arry:
        i.join()
        cv = time.time()
        print(f"使用时间{float('%.3f' % (cv-cc))}s")

#进程池
if __name__ =='__main__':
    poo = Pool(4)#建池
    poo_arry = []
    for index in range(1, 7):
        p = poo.apply_async(requests_360, args=(index,))
        poo_arry.append(p)
    cv = time.time()
    poo.close()
    poo.join()
    for i in poo_arry:
        cc = time.time()
        time.sleep(0.5)
        print(f"进程池花费的时间{float('%.1f'%(cc-cv))}s")
